package com.zzz.robot201.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.zzz.robot201.R;
import com.zzz.robot201.app.config.AppContent;
import com.zzz.robot201.di.component.DaggerControlComponent;
import com.zzz.robot201.mvp.contract.ControlContract;
import com.zzz.robot201.mvp.presenter.ControlPresenter;
import com.zzz.robot201.mvp.ui.activity.MachineStatusActivity;
import com.zzz.robot201.mvp.ui.activity.SplashActivity;
import com.zzz.robot201.utils.SpUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 07/08/2020 22:46
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class ControlFragment extends BaseFragment<ControlPresenter> implements ControlContract.View {

    @BindView(R.id.ll_phone)
    LinearLayout llPhone;
    @BindView(R.id.ll_hank)
    LinearLayout llHank;
    @BindView(R.id.tv_control)
    TextView tvControl;
    @BindView(R.id.iv_display)
    ImageView ivDisplay;
    @BindView(R.id.iv_tg)
    ImageView ivTg;
    @BindView(R.id.iv_phone)
    ImageView ivPhone;
    @BindView(R.id.iv_hank)
    ImageView ivHank;
    @BindView(R.id.rl_hank)
    RelativeLayout rlHank;
    @BindView(R.id.rl_phone)
    RelativeLayout rlPhone;
    boolean isHank ;
    public static ControlFragment newInstance() {
        ControlFragment fragment = new ControlFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerControlComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_control, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        isHank = (boolean) SpUtils.get(getContext(), AppContent.SaveInfoKey.ISPHONE,true);
        if (isHank){
            rlHank.setBackgroundResource(R.drawable.set_control_h_bg);
            rlPhone.setBackgroundResource(R.drawable.set_control_bg);
            ivHank.setImageResource(R.drawable.ic_hank);
            ivPhone.setImageResource(R.drawable.ic_phone);
        }else {
            rlHank.setBackgroundResource(R.drawable.set_control_bg);
            rlPhone.setBackgroundResource(R.drawable.set_control_h_bg);
            ivHank.setImageResource(R.drawable.ic_hank);
            ivPhone.setImageResource(R.drawable.ic_phone);
        }
    }

    /**
     * 通过此方法可以使 Fragment 能够与外界做一些交互和通信, 比如说外部的 Activity 想让自己持有的某个 Fragment 对象执行一些方法,
     * 建议在有多个需要与外界交互的方法时, 统一传 {@link Message}, 通过 what 字段来区分不同的方法, 在 {@link #setData(Object)}
     * 方法中就可以 {@code switch} 做不同的操作, 这样就可以用统一的入口方法做多个不同的操作, 可以起到分发的作用
     * <p>
     * 调用此方法时请注意调用时 Fragment 的生命周期, 如果调用 {@link #setData(Object)} 方法时 {@link Fragment#onCreate(Bundle)} 还没执行
     * 但在 {@link #setData(Object)} 里却调用了 Presenter 的方法, 是会报空的, 因为 Dagger 注入是在 {@link Fragment#onCreate(Bundle)} 方法中执行的
     * 然后才创建的 Presenter, 如果要做一些初始化操作,可以不必让外部调用 {@link #setData(Object)}, 在 {@link #initData(Bundle)} 中初始化就可以了
     * <p>
     * Example usage:
     * <pre>
     * public void setData(@Nullable Object data) {
     *     if (data != null && data instanceof Message) {
     *         switch (((Message) data).what) {
     *             case 0:
     *                 loadData(((Message) data).arg1);
     *                 break;
     *             case 1:
     *                 refreshUI();
     *                 break;
     *             default:
     *                 //do something
     *                 break;
     *         }
     *     }
     * }
     *
     * // call setData(Object):
     * Message data = new Message();
     * data.what = 0;
     * data.arg1 = 1;
     * fragment.setData(data);
     * </pre>
     *
     * @param data 当不需要参数时 {@code data} 可以为 {@code null}
     */
    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    private boolean IsDisplay = false;
    private boolean isTg = false;

    @OnClick({R.id.iv_display, R.id.iv_tg,R.id.ll_phone,R.id.ll_hank})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_display:
                if (IsDisplay) {
                    IsDisplay = false;
                    ivDisplay.setImageResource(R.drawable.radio_box_unselect);
                } else {
                    IsDisplay = true;
                    ivDisplay.setImageResource(R.drawable.radio_box_select);
                }
                break;
            case R.id.iv_tg:
                if (isTg) {
                    isTg = false;
                    ivTg.setImageResource(R.drawable.radio_box_unselect);
                } else {
                    isTg = true;
                    ivTg.setImageResource(R.drawable.radio_box_select);
                }
                break;

                case R.id.ll_phone:
                    rlHank.setBackgroundResource(R.drawable.set_control_h_bg);
                    rlPhone.setBackgroundResource(R.drawable.set_control_bg);
                    ivHank.setImageResource(R.drawable.ic_hank);
                    ivPhone.setImageResource(R.drawable.ic_phone);
                    SpUtils.put(getContext(), AppContent.SaveInfoKey.ISPHONE, true);
                    break;

                case R.id.ll_hank:
                    rlHank.setBackgroundResource(R.drawable.set_control_bg);
                    rlPhone.setBackgroundResource(R.drawable.set_control_h_bg);
                    ivHank.setImageResource(R.drawable.ic_hank);
                    ivPhone.setImageResource(R.drawable.ic_phone);
                    SpUtils.put(getContext(), AppContent.SaveInfoKey.ISPHONE, false);
                    break;
        }
    }
}
